

package Game;

import Game.Objects.Rock;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Random;

/**
 * This will be a default map for tanks to roam around on
 * Compiler: jdk1.6.0_14 <br />
 * IDE: NetBeans 6.7.1 <br />
 * @author Andrew Smith <br />
 */
public class Map
{
    //the absolute dimennsions that this map will be
    public static final int BOUNDS_WIDTH = Match.BOUNDS_WIDTH;
    public static final int BOUNDS_HEIGHT = Match.BOUNDS_HEIGHT;

    //start box areas for tanks. no rocks are to be drawn in here
    public static final Rectangle PLAYER_1_START_BOX = Match.PLAYER_1_START_BOX;
    public static final Rectangle PLAYER_2_START_BOX = Match.PLAYER_2_START_BOX;


    /** holds all the rock objects for this map */
    private ArrayList<Rock> rocks;



    /**
     * Creates a new map object to be used for a match
     */
    public Map()
    {
        initRocks();
    }



    /**
     * Init's the rocks and the locations
     */
    private void initRocks()
    {
        rocks = new ArrayList<Rock>();

        Random rangen = new Random();
        //can have a maximum of 17 rocks, minimum of 3 (14+3)
        int amtRocks = rangen.nextInt(14) + 3;

        for(int i=0; i<amtRocks; i++)
        {   //now checks to see if rocks are inside starting positions
            boolean done = false;
            while(!done)
            {
                int xPos = rangen.nextInt(BOUNDS_WIDTH);
                int yPos = rangen.nextInt(BOUNDS_HEIGHT);
                //if((xPos < (PLAYER_1_START_BOX.x+PLAYER_1_START_BOX.width)) &&
                //        yPos < (PLAYER_1_START_BOX.y+PLAYER_1_START_BOX.height)) //checks player 1 start box to make sure rock is not in it
                //{   //does nothing
                //}
                //else if((xPos < (PLAYER_2_START_BOX.x+PLAYER_2_START_BOX.width)) &&
                //        yPos < (PLAYER_2_START_BOX.y+PLAYER_2_START_BOX.height)) //checks player 2 start box to make sure rock is not in it
                //{   //does nothing
                //}
                //else
                {   rocks.add(new Rock(xPos, yPos));
                    done = true;
                }
            }
        }
    }


    /**
     * Checks this point to make sure that there are no obsticles at that current location
     * @param p the point to check
     * @return true if there are no obsticles at this location, false if there is or if it is outside of the map
     */
    public boolean validPoint(Point p)
    {   //checks to see if rock is at this current point
        for(int i=0; i<rocks.size(); i++)
        {
            if(rocks.get(i).contains(p))
            {   return false;
            }
        }
        //checks to see if point is outside of map
        if(p.x > BOUNDS_WIDTH || p.y > BOUNDS_HEIGHT)
        {   return false;
        }
        //if it gets past the checks...
        return true;
    }

    /**
     * Checks this coordinate to make sure that there are no obsticles at that current location
     * @param x the x coordinate
     * @param y the y coordinate
     * @return true if there are no obsticles at this location, false if there is or if it is outside of the map
     */
    public boolean validPoint(int x, int y)
    {   return validPoint(new Point(x,y));
    }






    /**
     * Draws this map on the graphics passed to it
     * @param g the graphics to draw on
     */
    public void paint(Graphics g)
    {
        for(int i=0; i<rocks.size(); i++)
        {
            rocks.get(i).draw(g);
        }
    }






}
